package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.Dto.DishDto;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.DishFlavor;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping
    public R<String> add(@RequestBody DishDto dishDto){
        //自定义saveWithFlavor方法
        dishService.saveWithFlavor(dishDto);

        redisTemplate.delete("dish_*");
        return R.success("添加成功");
    }
    @GetMapping("/page")
    public R<Page<DishDto>> page(Integer page,Integer pageSize,String name){
        Page<Dish> pageInfo1=new Page<>(page,pageSize);
        QueryWrapper<Dish> queryWrapper =new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name),"name",name);
        dishService.page(pageInfo1,queryWrapper);
        Page<DishDto> pageInfo2=new Page<>(page,pageSize);
        //创建pageInfo2的records集合 最后给他赋值
        ArrayList<DishDto> dtoList = new ArrayList<>();
        //把pageInfo1中除了records外的所有值给pageInfo2
        BeanUtils.copyProperties(pageInfo1,pageInfo2,"records");
        for (Dish dish : pageInfo1.getRecords()) {
            DishDto dishDto=new DishDto();
            BeanUtils.copyProperties(dish,dishDto);
            //根据ID查询对应的category对象
            Category category = categoryService.getById(dish.getCategoryId());
            //根据对象获取对应的名字
            dishDto.setCategoryName(category.getName());
            dtoList.add(dishDto);
        }
        pageInfo2.setRecords(dtoList);
        return R.success(pageInfo2);

    }
    @GetMapping("/{id}")
    public R<DishDto> findById(@PathVariable Long id){
        DishDto dishDto = dishService.getByIdWithFlavor(id);
        return R.success(dishDto);
    }
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        dishService.updateWithFlavor(dishDto);
        redisTemplate.delete("dish_*");
        return R.success("修改成功");
    }

    /**
     * 自己写删除 还没写完
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete( List<Long> ids){
        dishService.removeByIds(ids);

        return R.success("删除成功");
    }

    @GetMapping("/list")
    public R<List<DishDto>> list ( Long categoryId){
         List<DishDto> data = (List<DishDto>) redisTemplate.opsForValue().get("idsh" + categoryId);
        if(data!=null){
            return R.success(data);
        }
        Dish dish = dishService.getById(categoryId);
        QueryWrapper<Dish> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("category_id",categoryId);
        queryWrapper.eq("status",1);
        List<Dish> dishList = dishService.list(queryWrapper);
        //新建dishdto集合
        ArrayList<DishDto> dishDtoArrayList=new ArrayList<>();
        for (Dish dish1 : dishList) {
            DishDto dishDto = new DishDto();
            //将dish拷贝到dishdto中
            BeanUtils.copyProperties(dish1,dishDto);
            //利用dishFlavorService根据ID查询对应的口味
            QueryWrapper<DishFlavor> queryWrapper1=new QueryWrapper();
            queryWrapper1.eq("dish_id",dishDto.getId());
            List<DishFlavor> list = dishFlavorService.list(queryWrapper1);
            dishDto.setFlavors(list);
            dishDtoArrayList.add(dishDto);
        }
        redisTemplate.opsForValue().set("dish_"+categoryId,dishDtoArrayList,30, TimeUnit.MINUTES);
        return R.success(dishDtoArrayList);
    }
}

